package com.example.service;

import com.example.common.Result;
import com.example.entity.Admin;
import com.example.mapper.AdminMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AdminService {

    @Resource
    private AdminMapper adminMapper;
    public Result register(Admin admin) {
        Admin old = adminMapper.getByAccount(admin);
        if(old!=null){
            return Result.error("账号已被注册");
        }
        if(!adminMapper.add(admin)){
            return Result.error("注册失败");
        }
        return Result.success("注册成功");
    }

    public Result login(Admin admin) {
        Admin old = adminMapper.getByAccount(admin);
        if(admin.getPassword() == null){
            return Result.error("密码不能为空");
        }
        if(admin.getAccount() == null){
            return Result.error("账号不能为空");
        }
        if(old!=null){
            if(old.getPassword().equals(admin.getPassword())){
                return Result.success(old);
            }else {
                return Result.error("密码错误");
            }
        }else {
            return Result.error("账号不存在");
        }
    }
}
